package com.paradise.ytj.service;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.os.PowerManager;
import android.text.format.DateFormat;

import com.paradise.ytj.R;
import com.paradise.ytj.activity.MainActivity;
import com.paradise.ytj.activity.VideoActivity;
import com.paradise.ytj.bean.MessageEvent;
import com.paradise.ytj.helper.ContactDao;
import com.paradise.ytj.helper.RecordDao;
import com.paradise.ytj.receiver.ReceiverBroadcastReceiver;
import com.paradise.ytj.utils.CameraUtils;
import com.paradise.api.FaceCall;
import com.paradise.api.FacePhone;
import com.paradise.api.FacephoneListener;
import com.paradise.api.FacephonePreferences;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.linphone.core.LinphoneCall;
import org.linphone.core.LinphoneCore;

import java.util.Date;

public class IncommingService extends Service implements FacephoneListener.FacephoneCallStateListener {

    private String nameByAccount = "未知联系人";
    private ContactDao contactDao;

    public IncommingService() {

    }

    @Override
    public IBinder onBind(Intent intent) {

        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //设置自动接收视频请求
        contactDao = new ContactDao(this);
        FacephonePreferences.instance().setAutomaticallyAcceptVideoRequests(true);
        FacePhone.instance().addCallStateListener(this);
        EventBus.getDefault().register(this);
    }

    @Override
    public void callState(LinphoneCore linphoneCore, LinphoneCall linphoneCall, LinphoneCall.State state, String s, boolean isAutoAnswer, boolean isVideoCall) {
        if (CameraUtils.getNumber() == 0) {
            isAutoAnswer = false;
        }
        String account = linphoneCall.getRemoteAddress().getUserName();
        nameByAccount = contactDao.getNameByAccount(account);
        if (nameByAccount.equals("未知联系人")) {
            nameByAccount = linphoneCall.getRemoteAddress().getDisplayName();

        }
        if (state.equals(LinphoneCall.State.IncomingReceived)) {
            //接听来电唤醒屏幕
            PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "WakeAndLock");
            wakeLock.acquire();
            CameraUtils.stopCamera();
            if (isAutoAnswer) {
                FaceCall.instance().acceptCall();
                Intent intent = new Intent();
                intent.putExtra("inout", "in");
                intent.putExtra("object", nameByAccount);
                intent.putExtra("phone", account);
                intent.putExtra("auto", true);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setClass(getApplicationContext(), VideoActivity.class);
                startActivity(intent);
                FacePhone.instance().removeCallStateListener();
            } else {
                Intent intent = new Intent();
                intent.putExtra("inout", "in");
                intent.putExtra("auto", false);
                intent.putExtra("object", nameByAccount);
                intent.putExtra("phone", account);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setClass(getApplicationContext(), VideoActivity.class);
                startActivity(intent);
                FacePhone.instance().removeCallStateListener();

            }

        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void getFinish(MessageEvent messageEvent) {
        FacePhone.instance().addCallStateListener(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
// 在API11之后构建Notification的方式
        Notification.Builder builder = new Notification.Builder
                (this.getApplicationContext()); //获取一个Notification构造器
        Intent nfIntent = new Intent(this, MainActivity.class);
        builder.setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0)) // 设置PendingIntent
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.app_log)) // 设置下拉列表中的图标(大图标)
                .setContentTitle("天堂视讯") // 设置下拉列表里的标题
                .setSmallIcon(R.drawable.app_log) // 设置状态栏内的小图标
                .setContentText("Service启动") // 设置上下文内容
                .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间
        Notification notification = builder.build(); // 获取构建好的Notification
        notification.defaults = Notification.DEFAULT_VIBRATE; //设置为默认的声音
        startForeground(110, notification);
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
        Intent intent = new Intent(IncommingService.this, ReceiverBroadcastReceiver.class);
        sendBroadcast(intent);
        EventBus.getDefault().unregister(this);
    }

}
